<template>
  <div class="u-p-20">
    <el-card shadow="never">
      <div slot="header">
        <span>
          <b class="u-f-16">用户列表</b>
        </span>
        <!-- <el-button type="danger" size="small" icon="el-icon-delete" class="u-right u-5mt" round v-if="hasPerm('userDelCacheBtn')" @click="handleDelCache()">
                    添加
        </el-button>-->
      </div>

      <el-form :inline="true" :model="queryForm" size="small" class="u-text-center">
        <el-form-item label="账号状态">
          <el-select v-model="queryForm.status" clearable>
            <el-option label="全部" value></el-option>
            <el-option label="正常" value="1"></el-option>
            <el-option label="停用" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="queryForm.sex" clearable>
            <el-option label="全部" value></el-option>
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="2"></el-option>
            <el-option label="保密" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键词">
          <el-input v-model="queryForm.keyword" placeholder="输入昵称、手机查询" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getPageData(true)">查询</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" @sort-change="changeSort" ref="table" class="u-mt-10">
        <el-table-column label="ID" prop="id"></el-table-column>
        <el-table-column label="头像">
          <template slot-scope="scope">
            <el-image :src="scope.row.head_img" fit="cover" lazy style="width: 48px"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="昵称" prop="nick_name"></el-table-column>
        <el-table-column label="手机" prop="phone"></el-table-column>
        <el-table-column label="性别" prop="sex">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.sex === 1">男</el-tag>
            <el-tag v-else-if="scope.row.sex === 2">女</el-tag>
            <el-tag v-else>保密</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="所属推广员id" prop="top_user_id"></el-table-column>
        <el-table-column label="下单数" prop="order_num" sortable></el-table-column>
        <el-table-column label="消费金额" prop="cons_all" sortable></el-table-column>
        <el-table-column label="趣豆本金" prop="bean"></el-table-column>
        <el-table-column label="趣豆收益" prop="earnings_bean"></el-table-column>
        <el-table-column label="收益余额" prop="earnings_price"></el-table-column>

        <el-table-column label="是否是会员" prop="member_level">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.member_level === 0">无</el-tag>
            <el-tag v-else-if="scope.row.member_level === 1">会员</el-tag>
            <el-tag v-else-if="scope.row.member_level === 2">VIP</el-tag>
            <el-tag v-else-if="scope.row.member_level === 3">VIP1</el-tag>
            <el-tag v-else-if="scope.row.member_level === 4">VIP2</el-tag>
            <el-tag v-else-if="scope.row.member_level === 5">1星</el-tag>
            <el-tag v-else-if="scope.row.member_level === 6">2星</el-tag>
            <el-tag v-else-if="scope.row.member_level === 7">3星</el-tag>
            <el-tag v-else-if="scope.row.member_level === 8">4星</el-tag>
            <el-tag v-else-if="scope.row.member_level === 9">5星</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="账号状态">
          <template slot-scope="scope" v-if="hasPerm('userStatBtn')">
            <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="2" active-color="#13ce66"
              inactive-color="#ff4949" @change="
                clickHandleStatus(scope.row.id, scope.row.status, scope.row)
              "></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="注册时间" prop="create_time" width="180"></el-table-column>

        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button @click.native="onUserClearBind(scope.row)" type="text" size="small"
              v-if="hasPerm('userClearBind')">清除用户关系</el-button>
            <!-- <el-button @click.native="handleRoute(scope.row.id, 'userDetail')" v-if="hasPerm('userDetail')" type="text"
              size="small">用户详情</el-button>
            <el-button @click.native="handleDelete(scope.row.id)" v-if="hasPerm('userDelBtn')" type="text" size="small">
              删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <div class="u-mt-20 u-text-center">
        <el-pagination background @size-change="handleSizeChange" @current-change="handlePageChange"
          :current-page="currentPage" :page-sizes="[10, 20, 30, 40, 50]" :page-size="limit"
          layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
      </div>
    </el-card>
    <el-dialog title="修改备注" :visible.sync="is_name_remark_show" width="300px">
      <el-form :model="objForm" label-width="80px">
        <el-form-item label="备注名称">
          <el-input v-model="objForm.name_remark" type="text" placeholder="请输入备注名称">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="is_name_remark_show = false">取 消</el-button>
        <el-button type="primary" @click="submitNameRemark()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import commonPage from "@/mixins/common-page";

export default {
  mixins: [commonPage],
  props: {
    isprops: Boolean,
  },
  data() {
    return {
      apiName: "user",
      queryForm: {
        sex: "",
        status: "",
        keyword: "",
        order: "",
      },
      is_name_remark_show: false,
      objForm: {
        name_remark: "",
        id: 0,
      },
      index: 0,
      agencyList: [],
    };
  },
  methods: {
    onModRemark(e) {
      this.index = e.$index;
      let item = this.tableData[this.index];
      this.is_name_remark_show = true;
      this.objForm.name_remark = item.name_remark;
      this.objForm.id = item.id;
    },
    submitNameRemark() {
      let that = this;
      let id = this.objForm.id;
      let name = this.objForm.name_remark;
      that.$api[that.apiName].memberRemark(id, name).then((response) => {
        if (response.code == 1) {
          that.$message({
            type: "success",
            message: "操作成功!",
          });
          that.tableData[that.index].name_remark = name;
          that.is_name_remark_show = false;
        }
      });
    },
    onUserClearBind(row) {
      this.$confirm("确认要清除关系状态吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let that = this;
          that.$api[that.apiName].clearBind(row.id).then((response) => {
            if (response.code == 1) {
              that.$message({
                type: "success",
                message: "操作成功!",
              });
              row.top_user_id = 0;
            }
          });
        })
        .catch(() => {

        });
    },
    handleDetail(user) {
      this.$router
        .push({
          name: "userDetail",
          params: {
            user: user,
            page: this.currentPage,
          },
        })
        .catch(() => { });
    },
    clickHandleStatus(id, status, row) {
      this.$confirm("确认要更改状态吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.handleStatus(id, status);
        })
        .catch(() => {
          row.status = row.status == 1 ? 2 : 1;
        });
    },
    clickMemberStatus(id, status, row) {
      let that = this;
      this.$confirm("确认要更改状态吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          that.$api[that.apiName].memberStatus(id, status).then((response) => {
            if (response.code == 1) {
              that.$message({
                type: "success",
                message: "操作成功!",
              });
            }
          });
        })
        .catch(() => {
          row.member_level = row.member_level == 1 ? 0 : 1;
        });
    },
    select_data(row) {
      //选中数据
      this.$emit("user_select", row); //返回父组件
    },
    changeSort(val) {
      if (this.$refs.table) {
        this.$refs.table.clearSort();
      }
      let order = val.order;
      if (order == "ascending") {
        order = "asc";
      } else {
        order = "desc";
      }
      this.queryForm.order = val.prop + " " + order;
      let that = this;
      this.tableData = [];
      that.getPageData(true);
    },
    beforeOnLoad() {

    },
  },
};
</script>
